<template>
  <div class="edit-profile">
    <h2 class="text-2xl font-bold mb-6 text-center">修改个人信息</h2>
    <el-form :model="profileForm" :rules="rules" ref="profileFormRef" @submit.native.prevent="handleSubmit">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="profileForm.name" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="账号" prop="account">
        <el-input v-model="profileForm.account" placeholder="请输入账号" disabled></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="profileForm.password" type="password" placeholder="请输入新密码"></el-input>
      </el-form-item>
     <!--  <el-form-item label="角色" prop="role">
        <el-select v-model="profileForm.role" placeholder="请选择角色">
          <el-option label="管理员" value="admin"></el-option>
          <el-option label="普通用户" value="user"></el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" native-type="submit" class="w-full custom-button">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      profileForm: {
        name: '',
        account: '',
        password: '',
        // role: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        account: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
         ]//,
        // role: [
        //   { required: true, message: '请选择角色', trigger: 'change' }
        // ]
      }
    };
  },
  methods: {
    handleSubmit() {
      this.$refs.profileFormRef.validate((valid) => {
        if (valid) {
          this.$http.put('/users', this.profileForm)
            .then(response => {
              if (response.data) {
                this.$message.success('个人信息修改成功');
                // 刷新用户信息
                localStorage.setItem('username', this.profileForm.name);
                // this.$router.push({ name: 'profile' });
              } else {
                this.$message.error('修改失败，请重试');
              }
            })
            .catch(error => {
              this.$message.error('修改失败，请稍后重试');
              console.error('修改个人信息请求出错:', error);
            });
        } else {
          return false;
        }
      });
    }
  },
  created() {
    // 初始化表单数据
    this.profileForm.name = localStorage.getItem('username');
    this.profileForm.account = localStorage.getItem('username');
    // this.profileForm.role = localStorage.getItem('role') || 'user';
  }
};
</script>

<style scoped>
.edit-profile {
  max-width: 500px;
  margin: 0 auto;
  padding: 20px;
  border-radius: 12px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  background: rgba(255, 255, 255, 0.9);
}

.el-form-item {
  margin-bottom: 20px;
}

.custom-button {
  background: #2a5298;
  border-color: #2a5298;
  color: #ffffff;
  transition: background 0.3s ease, transform 0.2s ease;
}

.custom-button:hover {
  background: #1e3c72;
  border-color: #1e3c72;
  transform: scale(1.05);
}
</style>